import { getToken } from '@/utils/auth'
import { downloadFile } from '@/api/upload'
export const qiankunStatic =
  process.env.NODE_ENV === 'development'
    ? ''
    : `/${process.env.VUE_APP_BASE_SYS_NAME}`
export const baseImgUrl = `/api/opt-file/file/image?token=${getToken()}&fileId=`
export const matchType = (fileName) => {
  if (fileName) {
    // 后缀获取
    let suffic = ''
    // 获取类型结果
    let result = ''
    try {
      const fileArr = fileName.split('.')
      suffic = fileArr[fileArr.length - 1].toLowerCase()
    } catch (error) {
      suffic = ''
    }
    // 图片格式
    const imgList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
    // 进行图片匹配
    result = imgList.some((item) => {
      return item === suffic
    })
    if (result) {
      result = 'image'
      return result
    }
  }
}

export const fileDownload = (fileInfo) => {
  // const fileInfo = file.data ? file.data : file

  downloadFile({ fileId: fileInfo.fileId }).then((res) => {
    const a = document.createElement('a')
    a.href = URL.createObjectURL(res)
    a.download = fileInfo.fileName
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
  })

  // const isImage = matchType(fileInfo.fileName)

  // if (isImage) {
  // } else {
  //   downloadFile({ fileId: fileInfo.fileId }).then((res) => {
  //     const a = document.createElement('a')
  //     a.href = URL.createObjectURL(res)
  //     a.download = fileInfo.fileName
  //     document.body.appendChild(a)
  //     a.click()
  //     document.body.removeChild(a)
  //   })
  // }
}

// 二级制文件流下载
export const fileExport = (data, fileName) => {
  const blob = new Blob([data])
  const url = window.URL.createObjectURL(blob) // 创建下载的链接
  const link = document.createElement('a') // 创建a标签
  link.href = url
  link.download = fileName // 重命名文件
  document.body.appendChild(link)
  link.click()
  URL.revokeObjectURL(url) // 释放内存
  document.body.removeChild(link)
}
